What is camelcase?
The camelcase npm package is used to convert strings to camel case. This means it takes input strings with spaces, hyphens, underscores, or a mix of different delimiters and converts them into a string where the first letter of each word is capitalized except for the first word, and all the words are joined without spaces.
What are camelcase's main functionalities?
String Conversion to Camel Case
Converts a hyphenated string to camel case.
"foo-bar".pipe(camelCase); // 'fooBar'
Handling Multiple Delimiters
Converts a string with multiple delimiters to camel case.
"foo_bar--baz foo".pipe(camelCase); // 'fooBarBazFoo'
Preserving Consecutive Uppercase Characters
Converts a string to camel case while preserving consecutive uppercase characters.
"foo-BAR".pipe(camelCase({ pascalCase: false })); // 'fooBAR'
Converting to Pascal Case
Converts a string to pascal case, which is similar to camel case but with the first letter capitalized.
"foo-bar".pipe(camelCase({ pascalCase: true })); // 'FooBar'
Other packages similar to camelcase
change-case
A package that provides various case conversion utilities including camel case, snake case, pascal case, and more. It offers more comprehensive case conversion options compared to camelcase.
to-camel-case
A package that converts a string to camel case. It is similar to camelcase but is a smaller package with fewer features and customization options.
param-case
This package is designed to convert strings to param case (kebab case). While it serves a different purpose, it is related in the sense that it deals with string case conversion, just like camelcase.
camelcase
Convert a dash/dot/underscore/space separated string to camelCase or PascalCase: foo-bar
→ fooBar
Correctly handles Unicode strings.
If you use this on untrusted user input, don't forget to limit the length to something reasonable.
Install
$ npm install camelcase
If you need to support Firefox < 78, stay on version 5 as version 6 uses regex features not available in Firefox < 78.
Usage
const camelCase = require('camelcase');
camelCase('foo-bar');
camelCase('foo_bar');
camelCase('Foo-Bar');
camelCase('розовый_пушистый_единорог');
camelCase('Foo-Bar', {pascalCase: true});
camelCase('--foo.bar', {pascalCase: false});
camelCase('Foo-BAR', {preserveConsecutiveUppercase: true});
camelCase('fooBAR', {pascalCase: true, preserveConsecutiveUppercase: true}));
camelCase('foo bar');
console.log(process.argv[3]);
camelCase(process.argv[3]);
camelCase(['foo', 'bar']);
camelCase(['__foo__', '--bar'], {pascalCase: true});
camelCase(['foo', 'BAR'], {pascalCase: true, preserveConsecutiveUppercase: true})
camelCase('lorem-ipsum', {locale: 'en-US'});
API
camelCase(input, options?)
input
Type: string | string[]
String to convert to camel case.
options
Type: object
pascalCase
Type: boolean
Default: false
Uppercase the first character: foo-bar
→ FooBar
preserveConsecutiveUppercase
Type: boolean
Default: false
Preserve the consecutive uppercase characters: foo-BAR
→ FooBAR
.
locale
Type: false | string | string[]
Default: The host environment’s current locale.
The locale parameter indicates the locale to be used to convert to upper/lower case according to any locale-specific case mappings. If multiple locales are given in an array, the best available locale is used.
const camelCase = require('camelcase');
camelCase('lorem-ipsum', {locale: 'en-US'});
camelCase('lorem-ipsum', {locale: 'tr-TR'});
camelCase('lorem-ipsum', {locale: ['en-US', 'en-GB']});
camelCase('lorem-ipsum', {locale: ['tr', 'TR', 'tr-TR']});
Setting locale: false
ignores the platform locale and uses the Unicode Default Case Conversion algorithm:
const camelCase = require('camelcase');
camelCase('lorem-ipsum');
camelCase('lorem-ipsum', {locale: false});
camelcase for enterprise
Available as part of the Tidelift Subscription.
The maintainers of camelcase and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.
Related